package com.lib.image.preview;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.lib.image.ImageLoader;
import com.lib.image.R;

import java.util.ArrayList;

import com.photoview.PhotoView;
import com.photoview.PhotoViewAttacher;


/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImagePageAdapter extends PagerAdapter {

    private int screenWidth;
    private int screenHeight;
    private ArrayList<String> images;
    private Activity mActivity;
    public PhotoViewClickListener listener;

    public ImagePageAdapter(Activity activity, ArrayList<String> images) {
        this.mActivity = activity;
        this.images = images;
        screenWidth = activity.getResources().getDisplayMetrics().widthPixels;
        screenHeight = activity.getResources().getDisplayMetrics().heightPixels;
    }

    public void setData(ArrayList<String> images) {
        this.images = images;
    }

    public void setPhotoViewClickListener(PhotoViewClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_preview_image, container, false);
        PhotoView photoView = view.findViewById(R.id.image_view);
        loadUrlWithWG(photoView, images.get(position));
        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                if (listener != null) listener.OnPhotoTapListener(view, x, y);
            }

            @Override
            public void onOutsidePhotoTap() {
                if (listener != null) listener.OnPhotoTapListener(view, 0, 0);
            }
        });
        container.addView(view);
        return view;
    }

    private void loadUrlWithWG(ImageView imageView, String url) {
        if(TextUtils.isEmpty(url)) return;
        if(url.startsWith("http") || url.startsWith("https")) {
            ImageLoader.loadFitCenterImage(imageView, url, null, null, imageView, null);
            return;
        }
        ImageLoader.loadFitCenterImage(imageView, url, null, null, imageView, null);
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    public interface PhotoViewClickListener {
        void OnPhotoTapListener(View view, float v, float v1);
    }
}
